<template>
  <div class="root">
    <div class="login">
      <h2>系统登录</h2>
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="loginForm"
        class="demo-loginForm"
        size="mini"
      >
        <el-form-item prop="name">
          <el-input
            v-model="loginForm.name"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            :type="hidden ? 'password' : 'text'"
            v-model="loginForm.pass"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
          >
            <template #suffix>
              <i
                class="iconfont"
                :class="hidden ? 'icon-eyes' : 'icon-eyes-'"
                @click="hidden = !hidden"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '../api/users.js'
import local from '../utils/local'
export default {
  data() {
    var checkName = (rule, value, callback) => {
      if (value === '') {
        return callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    var checkPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        name: '',
        pass: ''
      },
      rules: {
        name: [{ validator: checkName, trigger: 'blur' }],
        pass: [{ validator: checkPass, trigger: 'blur' }]
      },
      // 掩藏显示密码
      hidden: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const data = await login({
            account: this.loginForm.name,
            password: this.loginForm.pass
          })
          // console.log(data)
          if (data.code === 0) {
            this.$message({
              showClose: true,
              message: data.msg,
              type: 'success'
            })
            setTimeout(() => {
              this.$router.push('/home')
            }, 2000)
            // 将得到的token存储到本地数据
            local.set('token', data.token)
            local.set('role', data.role)
          } else {
            this.$message({
              message: data.msg,
              type: 'error'
            })
          }
        } else {
          this.$message({
            showClose: true,
            message: '账号与密码不匹配,请重新输出',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.root {
  display: flex;
  height: 100%;
  background-color: #2d3a4b;
  justify-content: center;
  align-items: center;
  .login {
    width: 300px;
    h2 {
      text-align: center;
      color: #fff;
      font-size: 16px;
      letter-spacing: 4px;
      margin-bottom: 20px;
    }
    .el-button {
      width: 100%;
    }
  }
}
</style>
